import logging

from django.core.management.base import BaseCommand

from application import settings

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    """
    项目初始化命令: python manage.py init
    """

    @classmethod
    def add_arguments(cls, parser):
        parser.add_argument(
            "init_name",
            nargs="*",
            type=str,
        )
        parser.add_argument("-y", nargs="*")
        parser.add_argument("-Y", nargs="*")
        parser.add_argument("-n", nargs="*")
        parser.add_argument("-N", nargs="*")

    @classmethod
    def handle(cls, *args, **options):
        reset = False
        if isinstance(options.get("y"), list) or isinstance(options.get("Y"), list):
            reset = True
        if isinstance(options.get("n"), list) or isinstance(options.get("N"), list):
            reset = False

        from admin.system.fixtures.initialize import Initialize

        initialize = Initialize()
        initialize.run(reset, "admin.system")

#         for app in settings.INSTALLED_APPS:
#             print(app)
#             try:
#                 exec(
#                     f"""
# from {app}.fixtures.initialize import Initialize
# Initialize(reset={reset},app="{app}").run()
#                     """
#                 )
#             except ModuleNotFoundError:
#                 # 兼容之前版本初始化
#                 try:
#                     exec(
#                         f"""
# from {app}.initialize import main
# main(reset={reset})
#                         """
#                     )
#                 except ModuleNotFoundError:
#                     pass
        print("初始化数据完成！")
